/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ","ajaxServ","datagridServ","sysmanMain","btnServ","bizProto","valueUtil","formServ"], 
	function($,msgServ,ajaxServ,datagridServ,sysmanMain,btnServ,bizProto,valueUtil,formServ) {

	'use strict';

	var    $detaildg,$detailgrid,$edit_window,_defaultConfig,
			$panel,_bizProto,_moduleConfig,_buttonConfig,
			$edit_window_height=700,$edit_window_width=900,
			docdetailEditUrl,
			doctypeQryUrl='/departmentHis/query/getTreeList.action',
			docdetailQryUrl='/medicalInpatient/query/getListDomain.action',
			
			_defaultConfig ={
				doc:null,
				rootId:null,				
				panelSelector:'div.well',
				subSystemCode:'sysman',
				moduleCode:'deptPatientSelect',
				btnSelector:' div.td-detail',
				doccode:null
			}
		   	
    
	var	private_method = {
		init : function(_options) {
			
			_moduleConfig = sysmanMain.getCurrentModuConfig();
			
			if (window.console) {
				console.info(_moduleConfig.fullBizcode +' modules gets ready and been loaded.');
			}
			
					
			_defaultConfig = $.extend({},_defaultConfig,_options)			
			_bizProto = new bizProto(_defaultConfig);			
			
			
			$edit_window = $('#win-dept-patient');
				
			_buttonConfig={
				"选择":private_method.select,
				"重置":private_method.reset,
			    "取消":private_method.cancel,
			    "查询":private_method.doSearch
			}
			
			
			private_method.initElement(_bizProto.pageDoc)
		},
		
		
		initElement: function (doc) {
			
			
			
			//初始化操作按钮
			var listTd = $(doc).find(_defaultConfig.btnSelector)					
			
			valueUtil.setMonthDate({target:$(doc).find('#inhzospitalat')})
			$detaildg = $(doc).find("table.dg-detail");
			
			$detailgrid=$($detaildg).datagrid({
				height :_bizProto.gridHeight,
				url : _bizProto.rootPath + docdetailQryUrl,
				toolbar:$(listTd)
			});
			
			private_method.bindBtnEvent()
			
		},	
		
		//获取查询参数
		getSearchParam:function(){
			var doc = _bizProto.pageDoc
			
			var $form = $(_bizProto.pageDoc).find('form.condition-form')
			
			var _conditionParam = formServ.getDataObj({form:$form})
			
			
			return _conditionParam
		},
		//绑定按钮事件
		bindBtnEvent:function(){
			// 绑定按钮事件
			var btn = $(_bizProto.pageDoc).find(_defaultConfig.btnSelector +' a.function-btn')
			$(btn).unbind('click');
			$(btn).bind('click', private_method.function_btn_listenner)			
			
			
		},
		// 功能按钮监听函数
		function_btn_listenner : function(e) {

			btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

		},
		// 新增函数
		select : function() {
			var selectedRecord =$($detailgrid).datagrid('getSelected')
			if(!selectedRecord){
				msgServ.Msgslide('请选择住院记录');
				return
			}
			
			var _tmpId =$($edit_window).attr('callback-id')
			var _tmp_patient_id =$($edit_window).attr('callback-patient_id')
			var _tmp_zyhm =$($edit_window).attr('callback-zyhm')
			var _tmp_doct_name=$($edit_window).attr('callback-doct_name')
			var _tmp_doct_id=$($edit_window).attr('callback-doct_id')
			var _tmp_patient_name=$($edit_window).attr('callback-patient_name')
			var _tmp_doct_depart_name =$($edit_window).attr('callback-doct_depart_name')
			var _tmp_doct_depart_code=$($edit_window).attr('callback-doct_depart_code')
			
			var _tmpElement
			_tmpElement =$('#' +_tmpId)
			$('#' +_tmpId).val(selectedRecord.id)
			
			_tmpElement =$('#' +_tmpId)
			$('#' +_tmp_doct_id).val(selectedRecord.doct_id)
			
			_tmpElement = $('#'+_tmp_doct_depart_code)
			$('#'+_tmp_doct_depart_code).val(selectedRecord.doct_depart_code)
			
			_tmpElement = $('#'+_tmp_patient_id)
			$('#'+_tmp_patient_id).textbox('setValue',selectedRecord.patient_id)
			
			_tmpElement = $('#'+_tmp_zyhm)
			$('#'+_tmp_zyhm).textbox('setValue',selectedRecord.zyhm)
			
			_tmpElement = $('#'+_tmp_doct_name)
			$('#'+_tmp_doct_name).textbox('setValue',selectedRecord.doct_name)
			
			_tmpElement = $('#'+_tmp_patient_name)
			$('#'+_tmp_patient_name).textbox('setValue',selectedRecord.patient_name)
			
			_tmpElement =$('#'+_tmp_doct_depart_name)
			$('#'+_tmp_doct_depart_name).textbox('setValue',selectedRecord.doct_depart_name)
			
			$edit_window.window('close')
		},
		// 编辑,flag 为form按钮可见性标志
		reset : function() {
			
			$detailgrid.datagrid('load')
								
		},
		
		doSearch:function(){
			var _param = private_method.getSearchParam()
			if(_param && !$.isEmptyObject(_param)){
				$detailgrid.datagrid('load',{condition:_param})
			}else{
				$detailgrid.datagrid('load')
			}
		},
		// 删除
		cancel : function() {
			$edit_window.window('close')
		}
	}
	
	var public_method = {
		/**
		 * @param _options.doc	页面根节点
		 * @param _options.docroot	当前业务页面根节点
		 */
		init:function(_options){
			
			private_method.init(_options)
		}
	}	
	return public_method;
});